(function (jQuery) {
    let lineListArray = new Array();

    $.sourceAnalyseUtils = {

        /**
         * 删除
         */
        delete: function () {
            $.messager.confirm("操作提示", "确定要删除吗？", function (ok) {
                if (ok) {
                    $.sourceAnalyseUtils.deleteSub();
                }
            });


        },

        deleteSub: function () {

            $.ajax({
                url: "source/deleteAnalyseLineDoList.json?Login_SessionId=" + sessionId,
                type: "post",
                contentType: "application/json",
                data: $.sourceAnalyseUtils.getSelectedRowsJson(),
                success: function (data) {
                    $.tableUtils.deleteSelections($.sourceAnalyseUtils.getTableDomInPage());
                    $.LmisUtils.showMessage($.i18n.prop("title"), $.i18n
                        .prop("删除成功"), 2000);
                }
            });

        },

        getSelectedRowsJson: function () {

            let rows = $.tableUtils.getSelections($.sourceAnalyseUtils.getTableDomInPage());
            return JSON.stringify(rows);

        },
        exportAnalyseOne: function () {
            let id = $.sourceAnalyseUtils.getPageData().oldId;
            if ($.StringUtils.isBlank(id)) {
                $.LmisUtils.showMessage($.i18n.prop("title"), $.i18n
                    .prop("还没有保存过"), 2000);
            }
            $.sourceAnalyseUtils.edit("endEdit");
            //先保存一下,再导出信息
            $.ajax({
                url: "source/saveAnalyse.json?Login_SessionId=" + sessionId,
                type: "post",
                contentType: "application/json",
                data: $.sourceAnalyseUtils.getPageDataJson(),
                async: false,
                success: function (data) {
                    $.sourceAnalyseUtils.refreshTable(data);
                    console.log("保存成功:" + data);
                }
            });
            let param = {id: id};
            let pageBean = JSON.stringify(param);
            window.location.href = 'source/exportAnalyse?'
                + sessionId + '&pageBean=' + encodeURI(pageBean);
        },

        save: function () {
            $.sourceAnalyseUtils.edit("endEdit");
            $.ajax({
                url: "source/saveAnalyse.json?Login_SessionId=" + sessionId,
                type: "post",
                contentType: "application/json",
                data: $.sourceAnalyseUtils.getPageDataJson(),
                success: function (restData) {
                    $.LmisUtils.showMessage($.i18n.prop("title"), $.i18n
                        .prop("保存成功"), 2000);
                    $.LmisUtils.closeOpeningWindow('#sourceAnalyseList_window');
                    $('#sourceAnalyseListTable').datagrid('reload');
                }
            });
        },

        getFormData: function () {
            return $.sourceAnalyseUtils.getFormDomInPage().form().serializeObject();
        },
        getPageData: function () {
            let pageData = $.sourceAnalyseUtils.getFormData();
            pageData.codeString = $.sourceAnalyseUtils.getDomInPage("#codeString").textbox("getValue");
            pageData.rows = $.tableUtils.getRows($.sourceAnalyseUtils.getTableDomInPage());
            return pageData;
        },
        getPageDataJson: function () {
            let pageData = $.sourceAnalyseUtils.getPageData();
            return JSON.stringify(pageData);
        },

        getDomInPage: function (selector) {
            let pageDom = $('#sourceAnalysePage');
            return pageDom.find(selector);
        },


        analyse: function () {
            $.ajax({
                url: "source/addAnalyse.json?Login_SessionId=" + sessionId,
                type: "post",
                contentType: "application/json",
                data: $.sourceAnalyseUtils.getPageDataJson(),
                success: function (data) {
                    $.sourceAnalyseUtils.refreshTable(data);
                }
            });

        },


        /**
         * 获取多行
         * @returns {any[]}
         */
        getRows: function () {
            let rows = new Array();
            for (let i = 0; i < lineListArray.length; i++) {
                rows[i] = $.sourceAnalyseUtils.getRow(lineListArray[i]);
            }
            return rows;

        }
        ,
        /**
         * 获取一行
         * @param lineList
         */
        getRow: function (lineList) {
            let row = new Object();
            for (let i = 0; i < lineList.length; i++) {
                let str = 'row.' + $.sourceAnalyseUtils.getTitleStr(i) + '=lineList[i]';
                eval(str);
            }
            return row;

        }
        ,
        getTitleStr: function (i) {
            return "title_" + i.toString();
        },

        getTitleFrozen: function (rows) {

            let title = new Array();
            title.push({
                field: 'ck',
                checkbox: true
            });
            if (rows == null || rows.length == 0) {
                return title;
            }

            for (let i = 0; i < rows.length; i++) {
                if (i % 2 == 0) {
                    $.sourceAnalyseUtils.titlePush(title, rows[i]);
                }
                if (i % 2 == 1) {
                    $.sourceAnalyseUtils.titlePushEditor(title, rows[i]);
                }
            }
            return title;
        },


        getTitle: function (rows) {

            let title = new Array();
            if (rows == null || rows.length == 0) {
                return title;
            }
            for (let i = 0; i < rows.length; i++) {
                if (i % 2 == 0) {
                    $.sourceAnalyseUtils.titlePush(title, rows[i]);
                }
                if (i % 2 == 1) {
                    $.sourceAnalyseUtils.titlePushEditor(title, rows[i]);
                }
            }
            return title;
        },


        titlePushEditor: function (title, titleStr) {

            title.push(
                {
                    field: titleStr,
                    title: titleStr,
                    align: 'right',
                    editor: {
                        type: 'validatebox',
                        options: {}
                    },
                    formatter: function (value, row, index) {
                        return value;
                    }
                }
            );
        },

        titlePush: function (title, titleStr) {

            title.push(
                {
                    field: titleStr,
                    title: titleStr,
                    align: 'right',
                    formatter: function (value, row, index) {
                        return value;
                    }
                }
            );
        },

        refreshTable: function (data) {

            $.sourceAnalyseUtils.getTableDomInPage().datagrid({
                frozenColumns: [$.sourceAnalyseUtils.getTitleFrozen(data.frozenTitle)],    //冻结前两列
                columns: [$.sourceAnalyseUtils.getTitle(data.title)]    //动态取标题
            });
            $.sourceAnalyseUtils.getTableDomInPage().datagrid('loadData', data.rows);  //动态取数据

        }
        ,


        getTableDomInPage: function () {
            return $.sourceAnalyseUtils.getDomInPage('#sourceAnalyseTable');
        },

        getFormDomInPage: function () {
            return $.sourceAnalyseUtils.getDomInPage('#sourceAnalysePage_form');
        },
        // 初始化方法
        initTable: function () {// 创建订单商品列表DataGrid
            $.sourceAnalyseUtils.getTableDomInPage().datagrid({
                method: 'get',
                iconCls: 'icon-large-smartart',
                striped: true,
                rownumbers: true,
                menuSetFunc: null,
                // fitColumns: true,
                menuId: '#sourceAnalysePage_menu',
                menuSetFunc: $.sourceAnalyseUtils.setMenuStatus,
                toolbar: $.sourceAnalyseUtils.toolbar()
            }).datagrid("columnMoving");
        },

        setMenuStatus: function () {

        },

        initPageData: function () {
            if ($.StringUtils.isBlank($.sourceAnalyseUtils.getFormData().oldId)) {
                return;
            }
            $.ajax({
                url: "source/findAnalyse.json?Login_SessionId=" + sessionId,
                type: "post",
                contentType: "application/json",
                data: $.sourceAnalyseUtils.getPageDataJson(),
                success: function (data) {
                    $.sourceAnalyseUtils.getFormDomInPage().form('load', data);
                    $.sourceAnalyseUtils.initRows(data.id);
                }
            })
            ;


        },

        initRows: function () {

            $.ajax({
                url: "source/pageAnalyseLine.json?Login_SessionId=" + sessionId,
                type: "post",
                contentType: "application/json",
                data: $.sourceAnalyseUtils.getPageDataJson(),
                success: function (data) {
                    $.sourceAnalyseUtils.refreshTable(data);
                }
            });


        },
        init: function () {
            $.sourceAnalyseUtils.initTable();
            $.sourceAnalyseUtils.getDomInPage("#codeString").textbox({});
            $.sourceAnalyseUtils.initPageData();
        },


        edit: function (key) {
            let hasSelect = $.sourceAnalyseUtils.getTableDomInPage().datagrid("getSelections");
            if (hasSelect.length <= 0) {
                return;
            }
            for (let i = 0; i < hasSelect.length; i++) {
                let row = hasSelect[i];
                let rowNumber = $.sourceAnalyseUtils.getTableDomInPage().datagrid("getRowIndex", row);
                $.sourceAnalyseUtils.getTableDomInPage().datagrid(key, rowNumber);
            }
        },

        toolbar: function () {
            let toolbar = new Array();
            toolbar.push(
                {
                    text: '添加数据',
                    iconCls: 'icon-add',
                    handler: function () {
                        $.sourceAnalyseUtils.getTableDomInPage().datagrid("insertRow", {
                            index: 0,
                            row: {}
                        });
                        $.sourceAnalyseUtils.getTableDomInPage().datagrid("beginEdit", 0);
                    }
                }
            );

            toolbar.push({
                text: '编辑数据',
                iconCls: 'icon-edit',
                handler: function () {
                    $.sourceAnalyseUtils.edit("beginEdit");

                }
            });

            toolbar.push({
                text: '保存修改',
                iconCls: 'icon-save',
                handler: function () {
                    $.sourceAnalyseUtils.edit("endEdit");
                }
            });
            toolbar.push({
                text: '删除',
                iconCls: 'icon-save',
                handler: function () {
                    $.sourceAnalyseUtils.delete();
                }
            });
            return toolbar;

        }

    };
})(jQuery);

$.sourceAnalyseUtils.init();
